package tenant

import (
	"cgs-server/helper"
	"cgs-server/server"
	"cgs-server/server/base"
	"cgs-server/server/model"
	"go.mongodb.org/mongo-driver/bson"
	"io/ioutil"
	"net/http"
)

func init() {
	//server.Handle(http.MethodPost, "/api/Tenant/Add", AddTenant, server.None)
	//server.Handle(http.MethodPost, "/api/Tenant/Update", UpdateTenant, server.None)
	//server.Handle(http.MethodGet, "/api/Tenant/List", ListTenant, server.None)
	//server.Handle(http.MethodGet, "/api/Tenant/GetByTenantId", GetByTenantId, server.None)
	//server.Handle(http.MethodPost, "/api/Tenant/Delete", DeleteTenant, server.None)

	server.Post("/api/Tenant/Add", AddTenant, "添加租户", server.SuperAdministrator)
	server.Post("/api/Tenant/Update", UpdateTenant, "更新租户", server.SuperAdministrator)
	server.Get("/api/Tenant/List", ListTenant, "获取租户列表", server.SuperAdministrator)
	server.Get("/api/Tenant/GetByTenantId", GetByTenantId, "获取租户", server.SuperAdministrator)
	server.Post("/api/Tenant/Delete", DeleteTenant, "删除租户", server.SuperAdministrator)
}

// 添加租户
func AddTenant(w http.ResponseWriter, r *http.Request) {
	// 租户名称
	tenantName := r.FormValue("tenantName")
	username := r.FormValue("username")
	password := r.FormValue("password")
	if tenantName == "" || username == "" {
		_, _ = helper.WriteJSON(w, server.Fail("TenantName or username is not allowed to be empty."))
		return
	}
	/*if !helper.CheckPassword(password) {
		_, _ = helper.WriteJSON(w, server.Fail("password with "))
		return
	}*/

	tenantVo := &TenantVo{
		Tenant: &Tenant{
			TenantName: tenantName,
		},
		User: &model.User{
			Username: username,
			Password: password,
			Name:     username,
		},
	}
	dataUserModel := base.GenerateDataUserModel(tenantVo, r)

	tenantService := NewTenantService()
	/*err := tenantService.Init()
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}*/
	err := tenantService.Save(dataUserModel)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	_, _ = helper.WriteJSON(w, server.OK().WithData(bson.M{"tenantId": tenantVo.TenantId}))
}

func UpdateTenant(w http.ResponseWriter, r *http.Request) {
	r.Body = http.MaxBytesReader(w, r.Body, int64(100<<20))
	s, _ := ioutil.ReadAll(r.Body) //把  body 内容读入字符串 s
	tenant := &Tenant{}
	err := helper.FromJSON(s, tenant)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	tenantService := NewTenantService()
	err = tenantService.Init()
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	dataUserModel := base.GenerateDataUserModel(tenant, r)
	err = tenantService.Update(dataUserModel)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	_, _ = helper.WriteJSON(w, server.OK("Update successfully!"))
}

func ListTenant(w http.ResponseWriter, r *http.Request) {
	tenantService := NewTenantService()
	err := tenantService.Init()
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	dataUserModel := base.GenerateDataUserModel(&Tenant{}, r)
	data, err := tenantService.List(dataUserModel)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	_, _ = helper.WriteJSON(w, server.OK().WithData(data))
}

func GetByTenantId(w http.ResponseWriter, r *http.Request) {
	tenantId := r.FormValue("tenantId")
	//bsonId, err := primitive.ObjectIDFromHex(tenantId)
	if tenantId == ""{
		_, _ = helper.WriteJSON(w, server.Fail("TenantId is required."))
		return
	}

	tenantService := NewTenantService()
	err := tenantService.Init()
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	tenant := &Tenant{TenantId: tenantId}
	dataUserModel := base.GenerateDataUserModel(tenant, r)
	result, err := tenantService.GetByTenantId(dataUserModel)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	_, _ = helper.WriteJSON(w, server.OK().WithData(result))
}

func DeleteTenant(w http.ResponseWriter, r *http.Request) {
	tenantId := r.FormValue("tenantId")
	//bsonId, err := primitive.ObjectIDFromHex(tenantId)
	if tenantId == ""{
		_, _ = helper.WriteJSON(w, server.Fail("TenantId is required."))
		return
	}

	tenantService := NewTenantService()
	err := tenantService.Init()
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	tenant := &Tenant{
		TenantId: tenantId,
	}
	dataUserModel := base.GenerateDataUserModel(tenant, r)
	err = tenantService.RemoveLogic(dataUserModel)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail("Delete failed."))
		return
	}

	_, _ = helper.WriteJSON(w, server.OK("Delete successfully!"))
}
